<?php

namespace app\admin\controller\knscy;

use app\admin\model\KnscyGoods;
use app\admin\model\KnscyOrderLogs;
use app\admin\model\KnscyOrders;
use app\admin\model\KnscyOrderTicket;
use app\admin\model\KnscyTickets;
use app\common\controller\AdminController;
use app\common\service\GoodService;
use app\common\service\OrderService;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="knscy_orders")
 */
class Orders extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\KnscyOrders();

        $this->assign('getPayStatusList', $this->model->getPayStatusList());

        $this->assign('getCanRefundList', $this->model->getCanRefundList());

        $this->assign('getNeedCheckList', $this->model->getNeedCheckList());

        $this->assign('getStatusList', $this->model->getStatusList());
        $this->assign('statusLabel', $this->model->statusLabels());
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            //后台不能创建订单
//            try {
//                $save = $this->model->save($post);
//            } catch (\Exception $e) {
//                $this->error('保存失败:' . $e->getMessage());
//            }
//            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            //检测，如果订单支付成功过，不能修改；
            $ispay_log = KnscyOrderLogs::where('order_id', $id)->where('tag','order.process')->find();
            if($ispay_log){
                $this->error('已生效订单不支持修改状态');
            }
            try {
                $oldOrder = clone $row; //用clone方法得到旧的订单模型
                //同步更新订单进度；
                //$post['order_status'] = KnscyOrders::labelToStatus($post['status_label']);
                $save = $row->save($post);
                $row = $this->model->find($id);
                //修改状态
                OrderService::admin_change_order($row, $oldOrder);
            } catch (\Exception $e) {
                $this->error('保存失败' .$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $row['orderTicket'] = KnscyOrderTicket::where('order_id', $id)->find();
        $row['good'] = KnscyGoods::find($row['good_id']);

        $this->assign('ticketTypeList', KnscyTickets::getTicketTypeList());

        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * @NodeAnotation(title="日志")
     *
     */
    public function logs()
    {
        $id = $this->request->get('id');
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            $logModel = new KnscyOrderLogs();
            list($page, $limit, $where) = $this->buildTableParames();
            $where[] = ['order_id','=', $id];
            $count = $logModel
                ->where($where)
                ->count();
            $list = $logModel
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


}